package com.cat.model.web.action.api;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.cat.model.biz.service.api.UserService;
import com.cat.model.common.model.common.UserTokenVO;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.dao.mapper.UserInfoMapper;
import com.cat.model.web.action.base.BaseAction;

public class CompanyAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 604080577375901309L;
	
	private String token;
	private Integer page;
	private Integer size;
	
	private File businessLicense; //营业执照照片
	private File idCardPositive; 
	private File idCardBack; 
	private File idCardHolder; 
	private String businessLicenseFileName;
	private String idCardPositiveFileName;
	private String idCardBackFileName;
	private String idCardHolderFileName;
	
	private String name;//名字
	private String phone;//联系电话
	private Integer type;//类型
	private String businessLicenseNumber;//营业执照注册号
	private String addr;//地址
	
	
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private UserService userService;
	
	
	/**
	 * 公司列表
	 * */
	public void getCompanyList(){
		/*if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}*/
		
		print(userService.getCompanyList());
	}
	
	/**
	 * 公司认证查询
	 * */
	public void userAuthInfo(){
		if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		print(userService.userAuthInfo(userToken.getUserId()));
	}
	
	
	/**
	 * 公司提交认证
	 * */
	public void saveUserAuthInfo(){
		if(StringUtils.isEmpty(token) || StringUtils.isEmpty(name) || StringUtils.isEmpty(phone)
				|| StringUtils.isEmpty(businessLicenseNumber)|| StringUtils.isEmpty(addr)
				|| type==null){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		if(null == businessLicense ){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		/*if(null == idCardPositive ){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}*/
		try{
			print(userService.saveUserAuthInfo(userToken.getUserId(), businessLicense, businessLicenseFileName
					,idCardPositive,idCardPositiveFileName,name,phone,type,businessLicenseNumber,addr));
		}catch(Exception e){
			e.printStackTrace();
			print(ErrorCode.SERVER_ERROR.response());

		}
	}
	
	
	
	/**
	 * 个人提交认证
	 * */
	public void savePersonUserAuthInfo(){
		if(StringUtils.isEmpty(token) || StringUtils.isEmpty(name) || StringUtils.isEmpty(phone)
				|| StringUtils.isEmpty(addr)
				|| type==null){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		if(null == idCardPositive){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		if(null == idCardBack){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		try{
			print(userService.savePersonUserAuthInfo(userToken.getUserId(), idCardPositive, idCardPositiveFileName
					, idCardBack, idCardBackFileName
				,name,phone,type,businessLicenseNumber,addr));
		}catch(Exception e){
			e.printStackTrace();
			print(ErrorCode.SERVER_ERROR.response());

		}
		
	}
	
	
	
	/**
	 * 修改公司认证
	 * */
	public void editUserAuthInfo(){
		if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		if(null == businessLicense){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		try{
			print(userService.updateUserAuthInfo(userToken.getUserId(), businessLicense, businessLicenseFileName));

		}catch(Exception e){
			e.printStackTrace();
			print(ErrorCode.SERVER_ERROR.response());

		}
	}
	
	/**
	 * 修改从业人员认证
	 * */
	public void editEmployeeAuthInfo(){
		if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		if(null == idCardPositive || null == idCardBack || null==idCardHolder){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(userService.updateEmployeeAuthInfo(userToken.getUserId(), idCardPositive, idCardPositiveFileName, idCardBack, idCardBackFileName, idCardHolder, idCardHolderFileName));
	}
	
	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public File getBusinessLicense() {
		return businessLicense;
	}

	public void setBusinessLicense(File businessLicense) {
		this.businessLicense = businessLicense;
	}

	public String getBusinessLicenseFileName() {
		return businessLicenseFileName;
	}

	public void setBusinessLicenseFileName(String businessLicenseFileName) {
		this.businessLicenseFileName = businessLicenseFileName;
	}

	public File getIdCardPositive() {
		return idCardPositive;
	}

	public void setIdCardPositive(File idCardPositive) {
		this.idCardPositive = idCardPositive;
	}

	public File getIdCardBack() {
		return idCardBack;
	}

	public void setIdCardBack(File idCardBack) {
		this.idCardBack = idCardBack;
	}

	public File getIdCardHolder() {
		return idCardHolder;
	}

	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getPhone() {
		return phone;
	}


	public void setPhone(String phone) {
		this.phone = phone;
	}


	public Integer getType() {
		return type;
	}


	public void setType(Integer type) {
		this.type = type;
	}


	

	public String getBusinessLicenseNumber() {
		return businessLicenseNumber;
	}


	public void setBusinessLicenseNumber(String businessLicenseNumber) {
		this.businessLicenseNumber = businessLicenseNumber;
	}


	public String getAddr() {
		return addr;
	}


	public void setAddr(String addr) {
		this.addr = addr;
	}


	public void setIdCardHolder(File idCardHolder) {
		this.idCardHolder = idCardHolder;
	}

	public String getIdCardPositiveFileName() {
		return idCardPositiveFileName;
	}

	public void setIdCardPositiveFileName(String idCardPositiveFileName) {
		this.idCardPositiveFileName = idCardPositiveFileName;
	}

	public String getIdCardBackFileName() {
		return idCardBackFileName;
	}

	public void setIdCardBackFileName(String idCardBackFileName) {
		this.idCardBackFileName = idCardBackFileName;
	}

	public String getIdCardHolderFileName() {
		return idCardHolderFileName;
	}

	public void setIdCardHolderFileName(String idCardHolderFileName) {
		this.idCardHolderFileName = idCardHolderFileName;
	}

}
